package com.alibaba.moco.springboot;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 拦截器配置
 * 
 * @author wb-lj240895
 */
@Configuration
public class SpringConfigAdapter extends WebMvcConfigurerAdapter {

    private static final String[] PATTERNS_INCLUDE         = { "/moco/special/**", "/api/**", "/v1/**", "/system/**",
                                                               "/bus/**" };
    /*private static final String[] PATTERNS_EXCLUDE         = { "/api/**", "/v1/**", "/system/**", "/error", "/bus/**",
                                                               "/profile/**" };*/
    private static final String[] PATTERNS_ALL             = { "/**" };
    private static final String[] PATTERNS_EXCLUDE_COOKIES = { "/pages/*", "/profile/configs", "/error" };

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 多个拦截器组成一个拦截器链
        // addPathPatterns 用于添加拦截规则
        // excludePathPatterns 用户排除拦截
        registry.addInterceptor(new SessionInterceptor()).addPathPatterns(PATTERNS_ALL).excludePathPatterns(PATTERNS_EXCLUDE_COOKIES);
        registry.addInterceptor(new ScriptInterceptor()).addPathPatterns(PATTERNS_INCLUDE);
        //registry.addInterceptor(new ResourceInterceptor()).addPathPatterns(PATTERNS_ALL).excludePathPatterns(PATTERNS_EXCLUDE);
        super.addInterceptors(registry);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/pages/index.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }

}
